package app.itw.tison.cloud.order_service.controller;


import app.itw.tison.cloud.model.Order;
import app.itw.tison.cloud.model.OrderItem;
import app.itw.tison.cloud.model.Product;
import app.itw.tison.cloud.order_service.client.ProductClient;
import app.itw.tison.cloud.order_service.service.OrderItemService;
import app.itw.tison.cloud.order_service.service.OrderService;
import app.itw.tison.cloud.order_service.service.impl.OrderServiceImpl;
import app.itw.tison.cloud.utils.JSONUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单服务
 */
@RestController
@RequestMapping("/api/v1/order/item")
public class OrderItemController {

  @Resource
  private OrderItemService orderItemService;
  @Resource
  private OrderService orderService;
  @Resource
  private ProductClient productClient;

  @GetMapping("/list")
  public Object list() {
    List<Order> orderList = orderService.getOrderList();
    List<Map> orderItems = new ArrayList<>();
    for (Order order : orderList) {
      //获得订单明细
      List<OrderItem> items = orderItemService.getOrderItemByOID(order.getId());
      HashMap<String, Object> map = new HashMap<>();
      map.put(order.getId(), items);//oid 明细
      orderItems.add(map);
    }
    return orderItems;
  }

  @GetMapping("/get")
  public Object get(String id) {
    OrderItem orderItem = orderItemService.getOrderItemByID(id);
    return orderItem;
  }

  @GetMapping("/save")
  public Object save(String uid) {

    HashMap msgMap = new HashMap();
    //假设页面传递了商品ids
    List<HashMap<String, Object>> list = new ArrayList<>();
    for (int i = 1; i <= 5; i++) {
      HashMap map = new HashMap<>();
      map.put(i + "", i * 2);//商品id,数量
      list.add(map);
    }

    //调用商品服务去查询商品信息生成订单信息
    String json = null;
    List<Product> pList = null;
    try {
      //json = JSONUtils.obj2json(list);
      Object products = productClient.getProducts(list);
      if (products == null) {
        msgMap.put("msg", "商品数据为空");
        return msgMap;
      }
      pList = JSONUtils.json2list(products.toString(), Product.class);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(e);
      msgMap.put("msg", "下单失败");
      return msgMap;
    }

    Order order = new Order(UUID.randomUUID().toString(), uid, "请尽快为我安排发货吧!", 0, new Date());

    OrderItem orderItem = null;
    List<OrderItem> OrderItems = new ArrayList<>();
    int Amount = 0;//总金额 整数是为了分为单位
    for (int i = 0; i < pList.size(); i++) {
      Product p = pList.get(i);
      orderItem = new OrderItem();
      orderItem.setId((i + 1) + "");
      orderItem.setOid(order.getId());//订单od
      orderItem.setPrice(p.getPrice());//商品价格
      orderItem.setPid(p.getId());//商品id
      for (HashMap<String, Object> item : list) {
        for (String k : item.keySet()) {
          if (k.equals(p.getId())) {
            orderItem.setQty(Integer.parseInt(item.get(k).toString()));//添加数量
          }
        }
      }
      orderItem.setCreateTime(new Date());
      Amount += orderItem.getPrice();
      OrderItems.add(orderItem);
    }
    order.setAmount(Amount);
    orderItemService.Saves(OrderItems, order);
    msgMap.put("msg", "下单成功，恭喜你" + uid + ",订单号：" + order.getId());
    return msgMap;
  }
}
